//
//  WPHomeViewController.swift
//  WB
//
//  Created by 汪鹏 on 2016/12/14.
//  Copyright © 2016年 汪鹏. All rights reserved.
//

import UIKit

// 声明一个全局的常量,一般用priva来声明，保证其他地方不可以访问
private let K_ORIGINAL_CELLID = "HOMEORIGINALCELLLID" // 原创微博cell的唯一表示
private let K_REWEET_CELLID = "HOMEREWEETCELLLID" // 转发微博cell的唯一表示

class WPHomeViewController: WPBaseViewController {
    
    /// 懒加载微博列表的视图模型
    lazy var listViewModel:WPWBViewModel = WPWBViewModel()
    
    override func setupTableView() {
        
        super.setupTableView()
        
        // 设置导航栏
        setUpNav()
        
        // 注册cell
        //tableView?.register(UITableViewCell.self, forCellReuseIdentifier: K_CELLID)
        tableView?.register(UINib(nibName: "WPStatusCell", bundle: nil), forCellReuseIdentifier: K_ORIGINAL_CELLID)
        tableView?.register(UINib(nibName: "WPStatusRweetCell", bundle: nil), forCellReuseIdentifier: K_REWEET_CELLID)
        
        // 设置行高
        //tableView?.rowHeight = UITableViewAutomaticDimension // 自动行高
        
        tableView?.estimatedRowHeight = 300
        
        // 去除分隔线
        tableView?.separatorStyle = .none
    }
    
    
    override func loadData() {
        
        refreshControl?.beginRefreshing()
        
        // 加载微博数据
        listViewModel.loadWbStatus(isPullUp: isPullUp){ (isSuccess, shouldRefresh) -> () in
            
            if self.refreshControl?.isRefreshing == true {
                
                self.refreshControl?.endRefreshing()
            }
    
            self.isPullUp = false
            
            if shouldRefresh {
                
                self.tableView?.reloadData()
            }
            
        }
        
    }
    
    @objc private func titleButtonClick(button: UIButton) {
        
        
        button.isSelected = !button.isSelected
    }
    
    @objc private func goToFriend(){
        
        let testVC = WPTestViewController()
        
        navigationController?.pushViewController(testVC, animated: true)
    }
    
    
    /// 设置导航栏上的控件
    private func setUpNav() {
        
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(goToFriend))
        
        let button = WPTitleViewButton(title: WPNetManager.shareManager.userAccount.screen_name)
        
        button.addTarget(self, action: #selector(titleButtonClick), for: .touchUpInside)
        
        navItem.titleView = button

    }
}

// MARK:--实现tableView的数据源和代理
extension WPHomeViewController{
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return listViewModel.status.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        // 0.获取数据
        let viewModel = listViewModel.status[indexPath.row]
        
        let cellID = viewModel.statu.retweeted_status != nil ? K_REWEET_CELLID : K_ORIGINAL_CELLID
        
        // 1.创建cell
        let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! WPStatusCell
        
        // 2.设置cell
        cell.viewModel = viewModel
        
        // 3.返回cell
        return cell
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        let viewModel = listViewModel.status[indexPath.row]
        
        return viewModel.rowHeight
    }
    
}
